package com.zjhcsoft.auth.service.impl;

import com.zjhcsoft.auth.entity.Account;
import com.zjhcsoft.auth.entity.AppResource;
import com.zjhcsoft.auth.entity.UserCredential;
import com.zjhcsoft.auth.dao.UserCredentialMapper;
import com.zjhcsoft.auth.service.AccountService;
import com.zjhcsoft.auth.service.AppResourceService;
import com.zjhcsoft.auth.service.UserCredentialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户凭证表 服务实现类
 * </p>
 *
 * @author wzya
 * @since 2021-03-26
 */
@Service
public class UserCredentialServiceImpl extends ServiceImpl<UserCredentialMapper, UserCredential> implements UserCredentialService {

    @Resource
    AppResourceService appService;
    @Resource
    AccountService accountService;
    @Override
    public Account getAccountByCredential(String channelCode, String corporationCode, String userCredential) {
        AppResource app = appService.getAppResourceByChannelCodeAndCorporationCode(channelCode,corporationCode);
        List<Long> accountIds = this.lambdaQuery().eq(UserCredential::getCredential,userCredential).list().stream().map(credential ->credential.getAccountId()).collect(Collectors.toList());
        Account account = accountService.lambdaQuery().in(Account::getId,accountIds).eq(Account::getAppId,app.getId()).one();
        return account;
    }
}
